/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package br.com.dao;


import br.com.modelo.HospedeBean;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author Felipe
 */
public class HospedeDAO {
    
     private Connection connection;

    public HospedeDAO() {
        this.connection = new ConnectionFactory().getConnection();
    }

    public void adiciona(HospedeBean hb) {
        String sql, sql2;
        sql = "insert into CADASTRO "
                + "values(?,?,?,?,?,?,?,?,?,?)";
       

        try {
            PreparedStatement stmt = connection.prepareStatement(sql);
            stmt.setInt(1,hb.getCd_doc());
            stmt.setString(2,hb.getTp_doc());
            stmt.setString(3,hb.getNome());
            stmt.setString(4,hb.getEndereco());
            stmt.setString(5,hb.getCidade());
            stmt.setString(6,hb.getEstado());
            stmt.setString(7,hb.getProfissao());
            stmt.setString(8,hb.getData_nascimento());
            stmt.setString(9,hb.getSexo());
            stmt.setString(10,hb.getEstado_civil());
           stmt.executeUpdate();
                    stmt.close();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

      /*  try {
            PreparedStatement stmt2 = connection.prepareStatement(sql2);
            stmt2.setInt(1, hb.getCd_doc());
            
            stmt2.execute();
            stmt2.close();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }*/
    }

    public HospedeBean getHospede() {
         HospedeBean hb = new HospedeBean();
        try {
           
            PreparedStatement stmt = this.connection
                    .prepareStatement("SELECT * FROM CADASTRO WHERE ID_DOC_CADASTRO=?");
            ResultSet rs = stmt.executeQuery();
            while (rs.next()) {
                hb.setCd_doc(rs.getInt("ID_DOC_CADASTRO"));
                hb.setTp_doc(rs.getString("TP_DOC_CADASTRO"));
                hb.setNome(rs.getString("NM_CADASTRO"));
                hb.setEndereco(rs.getString("NM_ENDERECO_CADASTRO"));
                hb.setCidade(rs.getString("NM_CIDADE_CADASTRO"));
                hb.setEstado(rs.getString("NM_ESTADO_CADASTRO"));
                hb.setProfissao(rs.getString("NM_PROFISSAO_CADASTRO"));
                hb.setData_nascimento(rs.getString("DT_NASC_CADASTRO"));
                hb.setSexo(rs.getString("TP_SEXO_CADASTRO"));
                hb.setEstado_civil(rs.getString("TP_ESTADOCIVIL_CADASTRO"));
                
            }
               
           
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
       
            return hb;
    }
    
    
     public void altera(HospedeBean hb) {
                String sql, sql2;
		 sql = "UPDATE CADASTRO SET TP_DOC_CADASTRO=?,NM_CADASTRO=?,NM_ENDERECO_CADASTRO=?,"
                + "NM_CIDADE_CADASTRO=?,NM_ESTADO_CADASTRO=?,NM_PROFISSAO_CADASTRO=?,"
                + "DT_NASC_CADASTRO=?,TP_SEXO_CADASTRO=?,TP_ESTADOCIVIL_CADASTRO=?"
                        + " where ID_DOC_CADASTRO=?";
                 
                
                
		PreparedStatement stmt;
		try {
			stmt = connection.prepareStatement(sql);

			
                        stmt.setString(1, hb.getTp_doc());
			stmt.setString(2, hb.getNome());
			stmt.setString(3, hb.getEndereco());
                        stmt.setString(4, hb.getCidade());
                        stmt.setString(5, hb.getEstado());
                        stmt.setString(6, hb.getProfissao());
                        stmt.setString(7, hb.getData_nascimento());
                        stmt.setString(8, hb.getSexo());
                        stmt.setString(9, hb.getEstado_civil());
                        stmt.setInt(10, hb.getCd_doc());
                        
                        stmt.executeUpdate();
			stmt.close();
		} catch (SQLException e) {
			throw new RuntimeException(e);
		
                     
	}
               
     }
     
     public void remove(HospedeBean hb) {
		PreparedStatement stmt;
                try {
			stmt = connection.prepareStatement("delete from CADASTRO where ID_DOC_CADASTRO=?");
			stmt.setInt(1, hb.getCd_doc());
			stmt.execute();
			stmt.close();
		} catch (SQLException e) {
			throw new RuntimeException(e);
		}
		/*try {
			stmt = connection.prepareStatement("delete from CADASTRO where ID_DOC_CADASTRO=?");
			stmt.setInt(1, hb.getCd_doc());
			stmt.execute();
			stmt.close();
		} catch (SQLException e) {
			throw new RuntimeException(e);
		}*/
                
                

	}
     
      public List<HospedeBean> getLista() {
		try {
			//cria um list do tipo AptoBean
			List<HospedeBean> listadehospedes = new ArrayList<HospedeBean>();
			//cria objeto para injetar comando tipo select no banco
			PreparedStatement stmt = this.connection
					.prepareStatement("select * from CADASTRO");
			//objeto resultset consegue guardar valor encontrado
			ResultSet rs = stmt.executeQuery();
			while (rs.next()) {
				// Criando o objeto apartamento
				HospedeBean hb = new HospedeBean();
				
                                
                           hb.setCd_doc(rs.getInt("ID_DOC_CADASTRO"));
                hb.setTp_doc(rs.getString("TP_DOC_CADASTRO"));
                hb.setNome(rs.getString("NM_CADASTRO"));
                hb.setEndereco(rs.getString("NM_ENDERECO_CADASTRO"));
                hb.setCidade(rs.getString("NM_CIDADE_CADASTRO"));
                hb.setEstado(rs.getString("NM_ESTADO_CADASTRO"));
                hb.setProfissao(rs.getString("NM_PROFISSAO_CADASTRO"));
                hb.setData_nascimento(rs.getString("DT_NASC_CADASTRO"));
                hb.setSexo(rs.getString("TP_SEXO_CADASTRO"));
                 hb.setEstado_civil(rs.getString("TP_ESTADOCIVIL_CADASTRO"));
				
				// adicionando apartamento para a lista
				listadehospedes.add(hb);
			}
			rs.close();
			stmt.close();
			return listadehospedes;
		} catch (SQLException e) {
			throw new RuntimeException(e);
		}
   
    
}
}


